package com.lagou.rabbit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * created by wq at 2020/12/13
 */
@Configuration
public class RabbitMqConfig {

    public static final String orderQueue = "order.queue";
    public static final String orderExchange = "order.exchange";
    public static final String orderRoutingKey = "order.key";

    public static final String orderDelayQueue = "order.delay.queue";
    public static final String orderDelayExchange = "order.delay.exchange";
    public static final String orderDelayRoutingKey = "order.delay.key";

    @Bean
    public Queue paymentQueue() {
        Map<String, Object> arguments = new HashMap<String, Object>(2);
        arguments.put("x-message-ttl", 10000);
        // 设置队列所关联的死信交换器
        arguments.put("x-dead-letter-exchange", orderDelayExchange);
        // 设置队列所关联的死信交换器的routingKey
        arguments.put("x-dead-letter-routing-key", orderDelayRoutingKey);
        Queue queue = new Queue(orderQueue, true, false, false, arguments);
        return queue;
    }

    @Bean
    public Exchange paymentExchange() {
        return new DirectExchange(orderExchange, true, false, null);
    }

    @Bean
    public Queue paymentDlxQueue() {
        return new Queue(orderDelayQueue, true, false, false, null);
    }

    @Bean
    public Exchange paymentDlxExchange() {
        return new DirectExchange(orderDelayExchange, true, false, null);
    }


    @Bean
    public Binding paymentQueueBinding() {
        return BindingBuilder.bind(paymentQueue()).to(paymentExchange()).with(orderRoutingKey).noargs();
    }

    @Bean
    public Binding paymentDlxQueueBinding() {
        return BindingBuilder.bind(paymentDlxQueue()).to(paymentDlxExchange()).with(orderDelayRoutingKey).noargs();
    }
}
